package FSMDemo.entity.Harvester.states
{
	import FSMDemo.entity.Harvester.Harvester;
	
	import com.cheezeworld.AI.AISettings;
	import com.cheezeworld.AI.Behaviors.Arrive;
	import com.cheezeworld.AI.FSM.IState;
	import com.cheezeworld.math.Vector2D;
	
	import flash.events.EventDispatcher;

	public class GoToLocationState extends EventDispatcher implements IState
	{
		public function setEntity( a_entity:* ) : void
		{
			if( !(a_entity is Harvester) )
			{
				throw new Error( "<GoToLocationState> a_entity must be of type Harvester!" );
			}
			m_entity = a_entity;
		}
		
		public function getEntity() : *
		{
			return m_entity;
		}
		
		public function GoToLocationState( a_location:Vector2D, a_arriveDistance:Number )
		{
			m_destination = a_location.copy();
			m_arriveDistanceSq = a_arriveDistance * a_arriveDistance;
		}
		
		public function enter():void
		{	
			m_entity.steering.addBehavior( new Arrive( m_destination, AISettings.arriveFast ));
		}
		
		public function execute():void
		{
			if( m_destination.distanceSqTo( m_entity.actualPos ) < m_arriveDistanceSq )
			{
				m_entity.FSM.revertToPreviousState();
			}
		}
		
		public function exit():void
		{
			//m_entity.steering.clearBehaviors();
			m_entity.velocity.Set( 0, 0 );
		}
		
		private var m_entity:Harvester;
		private var m_destination:Vector2D;
		private var m_arriveDistanceSq:Number;
	}
}